<?php
declare (strict_types=1);

namespace app\common\service\category\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\logic\category\admin\Category as CategoryLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 课程分类接口类-后台
 */
class Category extends BaseService
{
    /**
     * 分类顶级数据
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-24
     *
     * @return   [type]                      [description]
     */
    public function getList()
    {
        $data = app(CategoryLogic::class)->getList();
        // 定义字段
        $keyList = [
            // 'id'          => ListItem::make()->title('ID'),
            // 'pid'         => ListItem::make()->title('父级ID'),
            'title'       => ListItem::make()->title('名称'),
            'update_time' => ListItem::make()->title('更新时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        $config            = SystemConfig::get('system.category', ['use_public' => 0]);
        $list->use_public  = $config['use_public'];
        $list->is_platform = request()->is_platform;

        return $list->send();
    }

    /**
     * 分类子集
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-24
     */
    public function getChildren($id)
    {
        $data = app(CategoryLogic::class)->getList($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 创建分类
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-24
     */
    public function createCategory()
    {
        $data = Request::post();
        $re   = app(CategoryLogic::class)->createCategory($data);

        return $this->parseToData($re, 1, '创建成功');
    }

    /**
     * 编辑分类
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-24
     */
    public function editCategory($id)
    {
        $data = Request::put();
        $re   = app(CategoryLogic::class)->editCategory($data);

        return $this->parseToData($re, 1, '编辑成功');
    }

    /**
     * 删除分类
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-24
     */
    public function deleteCategory($id)
    {
        $re = app(CategoryLogic::class)->deleteCategory($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 树形结构
     *
     * @return \think\Response
     */
    public function getTree()
    {
        $type = Request::route('category_type/d', 0);
        $data = app(CategoryLogic::class)->cascader(0, $type);

        return $this->parseToData($data, 1, '获取成功');
    }
}
